library flutter_plugin_demo;

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

part 'src/platform_text_widget.dart';

typedef void EventCallback(arg);

class FlutterPluginDemo {
  static const MethodChannel _channel =
      const MethodChannel('icu.guodapeng.flutter_plugin_demo');

  static StreamSubscription _eventSubs;
  static EventCallback _voidCallback;

  static void enableWatcher() async {
    if (_eventSubs == null) {
      _eventSubs = EventChannel('icu.guodapeng.flutter_plugin_demo/event')
          .receiveBroadcastStream()
          .listen(_eventListener, onError: _errorListener);
    }
  }

  static void disableWatcher() async {
    _eventSubs?.cancel();
    _eventSubs = null;
  }

  static void _eventListener(event) {
    _voidCallback(event['v']);
  }

  static void _errorListener(Object obj) {
    print("errorListener: $obj");
  }

  static void listener(EventCallback voidCallback) {
    _voidCallback = voidCallback;
  }

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<String> get testArg async {
    final String msg =
        await _channel.invokeMethod('testArg', {"value": "test Arg"});
    return msg;
  }

  static Future<String> get testArgArr async {
    final Map map =
        await _channel.invokeMethod('testArgArr', {"value": "test Arg Arr"});
    return map['msg'];
  }
}
